package com.dzwww.recommend.rule.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dzwww.recommend.rule.common.api.ApiResult;
import com.dzwww.recommend.rule.common.po.RuleConfig;
import com.dzwww.recommend.rule.service.common.RuleConfigService;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.ws.rs.core.MediaType;

/**
 * 规则配置管理api接口(旧版)
 * @author Administrator
 * 2019年7月19日
 */
@RestSchema(schemaId = "ruleConfig")
@RequestMapping(path = "/api/ruleConfig/management", produces = MediaType.APPLICATION_JSON)
public class RuleConfigManageMentController  {

    @Resource
    RuleConfigService ruleConfigService;

    /**
     * 添加规则配置
     */
    @PostMapping
    public ApiResult managementSave(@RequestBody RuleConfig ruleConfig){
        boolean add = ruleConfigService.add(ruleConfig);
        if(add){
            return ApiResult.succeed("添加成功");
        }
        return ApiResult.fail("","添加失败");
    }

    /**
     * 修改规则配置
     */
    @PutMapping
    public ApiResult managementUpdate(@RequestBody RuleConfig ruleConfig){
        boolean update = ruleConfigService.updateById(ruleConfig);
        if(update){
            return ApiResult.succeed("修改成功");
        }
        return ApiResult.fail("","修改失败");
    }

    /**
     * 分页查询
     * @return      返回分页参数
     */
    @GetMapping
    public ApiResult managementPage(Integer current, Integer size, @RequestParam(value = "name",required = false) String name){
        if(current < 0){
            return ApiResult.fail("","起始页不能小于零");
        }
        Page<RuleConfig> page = new Page<RuleConfig>();
        page.setCurrent(current);
        page.setSize(size);
        page = ruleConfigService.listPage(page, name);
        return ApiResult.succeed(page);
    }

    /**
     * 删除规则配置
     * @param id 删除的主键ID
     * @return 返回success
     */
    @DeleteMapping("/{id}")
    public ApiResult managementDelete(@PathVariable("id") Long id){
        boolean delete = ruleConfigService.delById(id);
        if (delete){
            return ApiResult.succeed("删除成功");
        }
        return ApiResult.fail("","删除失败");
    }

    /**
     * 启用禁用
     * @param ruleConfig  修改参数
     * @return  返回success
     */
    @PatchMapping
    public ApiResult managementPatch(@RequestBody RuleConfig ruleConfig){
        boolean patch = ruleConfigService.updateById(ruleConfig);
        if(patch){
            return ApiResult.succeed("操作成功");
        }
        return ApiResult.fail("","操作失败");
    }

    /**
     * 查询单个规则配置详情
     * @param id 唯一主键ID
     * @return  返回success
     */
    @GetMapping("/{id}")
    public ApiResult managementGetById(@PathVariable("id")Long id){
    	RuleConfig ruleConfig = ruleConfigService.getById(id);
        return ApiResult.succeed(ruleConfig);
    }
    
}